package com.dat255.internalstorage;

import java.util.ArrayList;
import java.util.Date;

import com.dat255.tentatime.Exam;

import android.content.Context;

/**
 * This class will facilitate all the methods
 * for FavoriteActivity
 * @author Joseph Hejderup
 * @version 1
 */
public class FavoritesManagement {
	
	private InternalStorage favStorage = null;
	private int numOfFavs;
	
	/**
	 * Initiates an management object to facilitate common tasks 
	 * for handling favourites
	 * @param application context
	 * @param number of favorites
	 */
	public FavoritesManagement(Context context, int numoffavs){
		
		
		favStorage = new InternalStorage(context);
		numOfFavs = numoffavs;

		
	}
	
	/**
	 * Saves the exam  for favorites
	 * @param exam
	 * @return true or false
	 */
	public boolean saveFavorite(Exam exam){
		
		if(this.listOfFavorites().size() < this.numOfFavs){
		
				return favStorage.addExam(exam);
		}else{
			
			return false;
		}
		
	}
	
	/**
	 * Deletes the selected exam in the favourite list
	 * @param true or false
	 * @return
	 */
	public boolean deleteFavorite(Exam exam){
		
		return favStorage.removeExam(exam);
		
		
	}
	/**
	 * Returns a list with all the favourites
	 * @return arraylist with all the favourites
	 */
	public ArrayList<Exam> listOfFavorites(){
		
		//remove old exams
		validateFavList();
		return favStorage.allExams();
		
		
		
		
	}
	/**
	 * This method will perform a check on the list to see whether the exam is outdated or not
	 */
	private void validateFavList(){
		
		//Todays date
		Date today = new Date();
		
		for(Exam exam: favStorage.allExams()){
			//Todays Date is after second:  > 0
			
			if(exam.getDateObject() != null){
			if(today.compareTo(exam.getDateObject()) > 0){
				
				favStorage.removeExam(exam);
				
			}
				
			}
	}

}
}
